<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="areaName">
        <el-select v-model="queryParams.areaName":filter-method="filterMethod" placeholder="请输入小区" filterable allow-create
                   clearable size="small" @change="getGroup(queryParams.areaName)" default-first-option>
          <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                     :value="item.areaName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼宇" prop="groupName">
        <el-select v-model="queryParams.groupName" placeholder="请输入楼宇" clearable size="small"  @change="getUnit(queryParams.areaName,queryParams.groupName)">
          <el-option v-for="(item, index) in groupIdOptions" :key="index" :label="item.groupName"
                     :value="item.groupName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元" prop="unitName">
        <el-select v-model="queryParams.unitName" placeholder="请输入单元" clearable size="small"  >
          <el-option v-for="(item, index) in unitNameOptions" :key="index" :label="item.unitName"
                     :value="item.unitName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="户" prop="doorName">
        <el-input
          v-model="queryParams.doorName"
          placeholder="请输入户名称"
          clearable size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程编号" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入工程编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修时间">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          size="small"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleBalance"
        >余额更新</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairQueryList" @selection-change="handleSelectionChange" style="width: 100%" height="305">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工程编号" align="center" prop="projectId" width="120"/>
      <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
      <el-table-column label="房屋编号" align="center" prop="houseId" />
      <el-table-column align="center" label="执行状态" prop="projectSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.REPAIR003" :value="scope.row.projectSts"/>
        </template>
      </el-table-column>
      <el-table-column label="工程名称" align="center" prop="projectName" width="200" />
      <el-table-column  :formatter="numberFormat"  label="资金余额" align="center" prop="balance" />
      <el-table-column  :formatter="numberFormat"  label="拨付金额" align="center" prop="payMoney" />
      <el-table-column  :formatter="numberFormat"  label="拨付余额" align="center" prop="payBalance" />
      <el-table-column  :formatter="numberFormat"  label="拨付利息" align="center" prop="payRate" />
      <el-table-column  :formatter="numberFormat"  label="分摊金额" align="center" prop="repairMoney" />
      <el-table-column :formatter="numberFormat"   label="不足金额" align="center" prop="noMoney" />
      <el-table-column label="余额比例(%)" align="center" prop="balanceRatio" />
      <el-table-column label="维修比例(%)" align="center" prop="repairRatio" />
      <el-table-column label="面积" align="center" prop="space" />
      <el-table-column label="执行日期" align="center" prop="executeDate" width="120"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRepairQuery,updateBalance} from "@/api/repair/repairQuery";
import {getAreaInfo, getAreaInfo1} from "@/api/house/area";
import {getGroupInfo} from "@/api/house/group";
import {getUnitInfo} from "@/api/house/unit";
export default {
  name: "RepairQueryT",
  dicts: ['REPAIR003'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 日期范围
      dateRange: [],
      // 总条数
      total: 0,
      // 维修记录查询表格数据
      repairQueryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        projectId: null,
        startDate:null,
        endDate:null,
      },
      areaIdOptions:[{}],
      groupIdOptions:[{}],
      unitNameOptions:[{}],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.filterMethod();
    this.areaSelect();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    filterMethod(val){
      this.value=val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect(){
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    getGroup(areaName){
      if(areaName!="") {
        getGroupInfo(areaName).then(response => {
          this.groupIdOptions = response.rows;
        });
      }
    },
    getUnit(areaName,groupName){
      getUnitInfo(areaName,groupName).then(response => {
        this.unitNameOptions = response.rows;
      });
    },
    /** 查询维修记录查询列表 */
    getList() {
      this.loading = true;
      listRepairQuery(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.repairQueryList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.areaSelect()
      });
    },
    handleBalance() {
      updateBalance().then(response => {
      this.$message.success("成功");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        houseId: null,
        projectId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        checkSts: null,
        repairMoney: null,
        updateMoney: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.areaSelect();
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/repair/repairQuery/export', {
        ...this.queryParams
      }, `维修查询_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
